package com.bytedance.improvingroad.activity

import android.util.Log
import androidx.annotation.IdRes
import com.bytedance.improvingroad.R
import com.bytedance.improvingroad.databinding.ActivityBottomNavBinding

class BottomNavActivity : BaseBindingActivity<ActivityBottomNavBinding>() {

    private var lastId = R.id.page1
    private var num = 0
    override fun initData() {

    }

    override fun initListener() {
        binding.bottomNav.setOnItemSelectedListener { item ->
            when(item.itemId) {
                R.id.page1 -> {
                    if (lastId != R.id.page1){
                        Log.e("cx","R.id.page1")
                        lastId = R.id.page1
                    } else {
                        showCountDrawable(R.id.page1)
                    }
                    true
                }
                R.id.page2 -> {
                    if (lastId != R.id.page2){
                        removeCountDrawable(R.id.page1)
                        Log.e("cx","R.id.page2")
                        lastId = R.id.page2
                        num = 0
                    }
                    true
                }
                else -> true
            }
        }
    }

    private fun showCountDrawable(@IdRes menuId: Int) {
        binding.bottomNav.getOrCreateBadge(menuId).apply {
            if (!isVisible) isVisible = true
            number = ++num
        }
    }

    private fun removeCountDrawable(@IdRes menuId: Int) {
        binding.bottomNav.getBadge(menuId).also { badgeDrawable ->
            if (badgeDrawable?.isVisible == true) {
                badgeDrawable.clearNumber()
                badgeDrawable.isVisible = false
            }
        }
    }

}